import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/AppViews/EngineerOrderView/model/meter_info_model.dart';
import '../../../app_files.dart';
import 'dart:convert' as convert;
import 'action.dart';
import 'state.dart';

Effect<MeterAddViewState> buildEffect() {
  return combineEffects(<Object, Effect<MeterAddViewState>>{
    MeterAddViewAction.onSubmit: _onSubmit,
    Lifecycle.initState: _initState,
  });
}

void _initState(Action action, Context<MeterAddViewState> ctx) async {
  ResultData res = await CommonNetWork.getMeterConfig();
  if (res.success) {
    print(res.data['卡表控制器']);
    List factoryList = res.data['卡表控制器'];
    List controlList = res.data['表具厂家'];
    for (var item in controlList) {
      for (var factory in factoryList) {
        if (CommonUtils.checkNullToString(factory['Key']) ==
            CommonUtils.checkNullToString(item['Value'])) {
          MeterFactoryModel factoryModel = MeterFactoryModel();
          factoryModel.meterFactory =
              CommonUtils.checkNullToString(factory['Key']);
          factoryModel.controlCode = CommonUtils.checkNullToString(item['Key']);
          String valueStr = CommonUtils.checkNullToString(factory['Value']);
          List<String> splits = valueStr.split('|');
          if (splits.length > 0) {
            List<String> meterUnitTypes = splits[0].split(',');
            for (String meterUnitType in meterUnitTypes) {
              List<String> value = meterUnitType.split('=');
              if (value.length > 1)
                factoryModel.meterUnitTypes.add({ value[1] : value[0]});
              else {
                factoryModel.meterUnitTypes.add({ 'flutter' : value[0]});
              }
            }
          }
          if (splits.length > 1) {
            List<String> meterSpecs = splits[1].split(',');
            for (String meterSpec in meterSpecs) {
              List<String> value = meterSpec.split('=');
              if (value.length > 1)
                factoryModel.meterSpecs.add({ value[0] : value[1]});
            }
          }
          factoryModel.meterType = splits.last;
          ctx.state.factoryItems.add(factoryModel);
        }
      }
    }
    ctx.dispatch(MeterAddViewActionCreator.onAction());
  }
}

void _onSubmit(Action action, Context<MeterAddViewState> ctx) async {
  String error = '';
  MeterInfoModel meterInfoModel = ctx.state.meterInfoModel;
  if (ctx.state.selectFactory == null) {
    error = '请选择表具类型';
  } else if (ctx.state.factoryItems[ctx.state.selectFactory].selectMeterSpec ==
      null) {
    error = '请选择气表种类';
  } else if (ctx.state.factoryItems[ctx.state.selectFactory].selectUnitType ==
      null) {
    error = '请选择母表类型';
  } else if (meterInfoModel.meterNo.isEmpty) {
    error = '请输入气表编号';
  } else if (meterInfoModel.meterBaseVolume.isEmpty) {
    error = '请输入初装表底';
  } else if (double.tryParse(meterInfoModel.meterBaseVolume) == null) {
    error = '请输入正确表底';
  } else if (meterInfoModel.meterBiaoxiang == null) {
    error = '请选择进气方向';
  } else if (meterInfoModel.isClosed == 1 &&
      meterInfoModel.meterFengNumber.isEmpty) {
    error = '请输入表具封号';
  }
  // else if (meterInfoModel.userIdNo == null) {
  //   error = '请输入用户身份证号';
  // }

  if (error.isNotEmpty) {
    CommonUtils.showErrorInfo(ctx.context, error);
    return;
  }
  await CommonUtils.showToast(ctx.context, '添加成功');
  Navigator.pop(ctx.context, meterInfoModel);
}
